package com.estock.common.model.task;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.estock.common.model.security.BaseEntity;
import com.estock.common.model.task.enums.TaskFrequency;
import com.estock.common.model.task.enums.TaskRunStatus;
import com.estock.common.util.DateUtil;

@Entity
@Table(name="Task")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Task extends BaseEntity {

	private static final long serialVersionUID = 7875211526011654420L;
	
	private String name;
	
	private String taskInstance;
	
	private String abbr;
	
	private int type;
	
	private String description;
	
	private int repeated;
	
	private int frequency;
	
	private String cronExpression;
	
	private Date nextSDateTime;
	
	private int runStatus;
	
	private int status;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTaskInstance() {
		return taskInstance;
	}

	public void setTaskInstance(String taskInstance) {
		this.taskInstance = taskInstance;
	}

	public String getAbbr() {
		return abbr;
	}

	public void setAbbr(String abbr) {
		this.abbr = abbr;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getRepeated() {
		return repeated;
	}

	public void setRepeated(int repeated) {
		this.repeated = repeated;
	}

	public int getFrequency() {
		return frequency;
	}

	public void setFrequency(int frequency) {
		this.frequency = frequency;
	}

	public String getCronExpression() {
		return cronExpression;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	public Date getNextSDateTime() {
		return nextSDateTime;
	}

	public void setNextSDateTime(Date nextSDateTime) {
		this.nextSDateTime = nextSDateTime;
	}
	
	public int getRunStatus() {
		return runStatus;
	}

	public void setRunStatus(int runStatus) {
		this.runStatus = runStatus;
	}
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	
	
	//////////////// Helper methods start ////////////////////

	/**
	 * necessary for show each rows' new sdate time.
	 * @return
	 */
	@Transient
	public String getNextSDateTimeStr() {
		return DateUtil.parseString(nextSDateTime, DateUtil.PATTERN_DATETIME_FORMAT);
	}
	@JsonIgnore
	public void setNextSDateTimeStr(String nextSDateTimeStr) {
	}
	
	@Transient
	public String getRunStatusLabel() {
		return TaskRunStatus.getDescByRunStatus(getRunStatus());
	}
	@JsonIgnore
	public void setRunStatusLabel(String runStatusLabel) {
	}
	
	@Transient
	public String getFrequencyLabel() {
		return TaskFrequency.getDescByFrequency(getFrequency());
	}
	@JsonIgnore
	public void setFrequencyLabel(String frequencyLabel) {
	}
	
	@Transient
	@JsonIgnore
	public int getRepeatedInternal() {
		switch (frequency) {
		case 2:
			return 60 * 60 * 24 * 1000; 
		case 3:
			return 60 * 60 * 24 * 7 * 1000; 
		}
		throw new RuntimeException("should not getRepeatedInternal when frequency is not daily and weekly");
	}
	
	@JsonIgnore
	public boolean isCustomFrequency() {
		return TaskFrequency.CUSTOM.getFrequency() == this.getFrequency();
	}
	
	////////////////Helper methods end ////////////////////

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof  Task)) return false;
		return this.getId() == ((Task)obj).getId();
	}
	
}
